package app.myproject.reader;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public abstract class AbstractPageReader {

	public StringBuffer content = new StringBuffer();
	
	public abstract void read(String link);
	
	public String process(String link) {
		read(link);
		return getResult();
	}
	
	public String getResult() {
		return content.toString();
	}

	public void write(String text) {
		content.append(text);
	}
	
	public void writeln(String text) {
		content.append(text + "\r\n");
	}
	
	public boolean connect(String link) {
		try {
			URL url = new URL(link);
			URLConnection uc = url.openConnection();
			if ( uc instanceof HttpURLConnection)
			{
			   HttpURLConnection httpConnection = (HttpURLConnection) uc;
			   uc.connect();
			   return (httpConnection.getResponseCode() == 200);
			}
		}
		catch(Exception e) {
			System.err.println(e.getMessage());
		}
		return false;
	}
}
